package Interface;
import java.awt.FlowLayout;
import java.awt.GraphicsEnvironment;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JRadioButton;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ListModel;

import javax.swing.WindowConstants;
import javax.swing.SwingUtilities;

import ChainArriere.*;
import java.util.*;
/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class MyJFrame extends javax.swing.JFrame {

	// Les variables de chainage arriere 
	private static backward kb = new backward();
	
	
	{
		//Set Look & Feel
		try {
			javax.swing.UIManager.setLookAndFeel("com.jgoodies.looks.plastic.Plastic3DLookAndFeel");
		} catch(Exception e) {
			e.printStackTrace();
		}
	}

	private JMenuBar jMenuBar1;
	private JLabel jLabel3;
	private JLabel jLabel4;
	private JLabel jLabel5;
	private JLabel jLabel1;
	private JMenuItem jMenuItem3;
	private JTextArea jTextArea3;
	private JComboBox jComboBox2;
	private JButton jButton5;
	private JButton jButton4;
	private JTextField jTextField2;
	private JButton jButton2;
	private JLabel jLabel2;
	private JTextArea jTextArea2;
	private JTextArea jTextArea1;
	private JMenuItem jMenuItem2;
	private JMenuItem jMenuItem1;
	private JMenu jMenu1;

	/**
	* Auto-generated main method to display this JFrame
	*/
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				MyJFrame inst = new MyJFrame();
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
			}
		});
	}
	
	public MyJFrame() {
		super();
		GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
		this.setExtendedState(this.getExtendedState() | this.MAXIMIZED_BOTH);
		initGUI();
	}
	
	private void initGUI() {
		try {
			GridBagLayout thisLayout = new GridBagLayout();
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			thisLayout.rowWeights = new double[] {0.1, 0.1, 0.1, 0.1, 0.1};
			thisLayout.rowHeights = new int[] {7, 7, 7, 7, 7};
			thisLayout.columnWeights = new double[] {0.1, 0.1, 0.1, 0.1, 0.1};
			thisLayout.columnWidths = new int[] {7, 7, 7, 7, 7};
			getContentPane().setLayout(thisLayout);
			{
				jTextArea1 = new JTextArea();
				JScrollPane scrollPane1 = new JScrollPane(jTextArea1);
				getContentPane().add(scrollPane1, new GridBagConstraints(1, 2, 2, 3, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(24, 0, 0, 0), 0, 0));
			}
			{
				jLabel2 = new JLabel();
				getContentPane().add(jLabel2, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
				jLabel2.setText("Question Proposée: ");
			}
			{
				jTextArea2 = new JTextArea();
				JScrollPane scrollPane2 = new JScrollPane(jTextArea2);
				getContentPane().add(scrollPane2, new GridBagConstraints(0, 0, 1, 4, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(20, 0, 0, 20), 0, 0));
			}
			{
				jButton2 = new JButton();
				getContentPane().add(jButton2, new GridBagConstraints(3, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
				jButton2.setText("RUN");
				jButton2.addActionListener(new ActionListener() 
				{

					@Override
					public void actionPerformed(ActionEvent arg0) 
					{
						// TODO Auto-generated method stub
						try
						{
							// Lancer la chainage arriere
							
							String demande = jComboBox2.getSelectedItem().toString();
							demande = demande.replace("\n","");
							
							String resul = kb.Run(demande);
							
							if (kb.ChaineResul == null && kb.arrete== false)
								jTextField2.setText(resul);
							
							jButton2.setBounds(1, 1,0,0);
						} catch (Exception e) 
						{
						    // TODO Auto-generated catch block
							e.printStackTrace();
						}
					}

				});						
			}
			{
				jTextField2 = new JTextField();
				getContentPane().add(jTextField2, new GridBagConstraints(2, 1, 1, 1, 0.0, 0.0, GridBagConstraints.NORTHWEST, GridBagConstraints.HORIZONTAL, new Insets(0, 5, 0, 15), 0, 0));
				jTextField2.setEditable(false);
			}
			{
				jLabel3 = new JLabel();
				getContentPane().add(jLabel3, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0, GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
				jLabel3.setText("Questions pertinentes :");
			}
			{
				jButton4 = new JButton();
				getContentPane().add(jButton4, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
				jButton4.setText("Vrai");
				jButton4.addActionListener(new ActionListener() 
				{

					@Override
					public void actionPerformed(ActionEvent arg0) 
					{
						// TODO Auto-generated method stub
						try
						{
							// Lancer la chainage arrier
							
							String resul = jTextField2.getText();							
							kb.setQuestionPertiente(resul,true);
							
							
							String demande = jComboBox2.getSelectedItem().toString();
							demande = demande.replace("\n","");
							
							
							resul = kb.Run(demande);							
							if (kb.ChaineResul == null && kb.arrete== false)
								jTextField2.setText(resul);
							else
							{
								jTextArea1.setText(kb.ChaineResul);
							}
							
							// Update Base de fait 
							Vector list = kb.listBaseDeFait();
							jTextArea3.setText("");
							for(int i = 0 ; i < list.size(); i++)
							{
								String temp = (String)list.elementAt(i);
								String []arr = temp.split(",");
								jTextArea3.append(arr[0]+":"+arr[1]+"\n");
							}
							
							
						} catch (Exception e) 
						{
						    // TODO Auto-generated catch block
							e.printStackTrace();
						}
					}

				});
			}
			{
				jButton5 = new JButton();
				getContentPane().add(jButton5, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
				jButton5.setText("Faux");
				jButton5.addActionListener(new ActionListener() 
				{

					@Override
					public void actionPerformed(ActionEvent arg0) 
					{
						// TODO Auto-generated method stub
						try
						{
							// Lancer la chainage arrier
							
							String resul = jTextField2.getText();							
							kb.setQuestionPertiente(resul,false);
							
							
							String demande = jComboBox2.getSelectedItem().toString();
							demande = demande.replace("\n","");
							
							
							resul = kb.Run(demande);							
							if (kb.ChaineResul == null && kb.arrete== false)
								jTextField2.setText(resul);
							else
							{
								jTextArea1.setText(kb.ChaineResul);
							}
							
							// Update Base de fait 
							Vector list = kb.listBaseDeFait();
							jTextArea3.setText("");
							for(int i = 0 ; i < list.size(); i++)
							{
								String temp = (String)list.elementAt(i);
								String []arr = temp.split(",");
								jTextArea3.append(arr[0]+":"+arr[1]+"\n");
							}
				
							
						} catch (Exception e) 
						{
						    // TODO Auto-generated catch block
							e.printStackTrace();
						}
					}

				});
			}
			{

				jComboBox2 = new JComboBox();
				getContentPane().add(jComboBox2, new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 13), 0, 0));
			}
			{
				jLabel4 = new JLabel();
				getContentPane().add(jLabel4, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
				jLabel4.setText("Liste de règles  : ");
			}
			{
				jTextArea3 = new JTextArea();
				JScrollPane scrollPane3 = new JScrollPane(jTextArea3);
				getContentPane().add(scrollPane3, new GridBagConstraints(4, 1, 1, 3, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(1, 12, 0, 1), 0, 0));
			}
			{
				jLabel5 = new JLabel();
				getContentPane().add(jLabel5, new GridBagConstraints(4, 0, 1, 1, 0.0, 0.0, GridBagConstraints.SOUTH, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
				jLabel5.setText("Liste base de faits :");
			}
			{
				jLabel1 = new JLabel();
				getContentPane().add(jLabel1, new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0, GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
				jLabel1.setText("Le résultat :");
			}
			{
				jMenuBar1 = new JMenuBar();
				setJMenuBar(jMenuBar1);
				{
					jMenu1 = new JMenu();
					jMenuBar1.add(jMenu1);
					jMenu1.setText("Choice");
					{
						jMenuItem1 = new JMenuItem();
						jMenu1.add(jMenuItem1);
						jMenuItem1.setText("Load File");
						jMenuItem1.addActionListener(new ActionListener() 
						{

							@Override
							public void actionPerformed(ActionEvent arg0) {
							// TODO Auto-generated method stub
							try {
								JFileChooser file = new JFileChooser();
								int retour = file.showOpenDialog(null);
								if(retour==JFileChooser.APPROVE_OPTION)
								{
									
									// Initalize l'ecran 
									 jTextArea2.removeAll();
									 jComboBox2.removeAllItems();
									 jTextArea3.removeAll();	
									
									
								   // un fichier a été choisi (sortie par OK)
								   // nom du fichier  choisi 
								   
								   String path = file.getSelectedFile().getPath();	
								   kb.Lire(path);				   
								   
								   // Affichage des regeles 
								   Vector listRegles = kb.listRegles();
								   for(int i = 0 ; i < listRegles.size(); i++)
								   {
									   jTextArea2.append((String)listRegles.elementAt(i) + "\n");
								   }
								   								   
								   // Affichage des question proposees
								   Vector listQuestionsProposees = kb.listQuestionProposee();
								   for(int i = 0 ; i < listRegles.size(); i++)
								   {
									   jComboBox2.addItem((String)listQuestionsProposees.elementAt(i) + "\n");
								   }	
								   
								}
							} catch (Exception e) {
							      // TODO Auto-generated catch block
									e.printStackTrace();
							}
							}
							});
					}
					{
						jMenuItem2 = new JMenuItem();
						jMenu1.add(jMenuItem2);
						jMenuItem2.setText("About this");
						jMenuItem2.addActionListener(new ActionListener() 
						{

							@Override
							public void actionPerformed(ActionEvent arg0) 
							{
								JOptionPane.showMessageDialog(null,
									    "HA Pham The Anh et Cheikh Mbacke thiam \n"+"Etudiants promo 16 à l'IFI ",
									    "Voici les concepteurs de cette application : ",
									    JOptionPane.PLAIN_MESSAGE);
								
							}	
									
						});			
					}
					{
						jMenuItem3 = new JMenuItem();
						jMenu1.add(jMenuItem3);
						jMenuItem3.setText("Exit");
						jMenuItem3.addActionListener(new ActionListener() 
						{

							@Override
							public void actionPerformed(ActionEvent arg0) 
							{
								dispose();
							}

						});	
					}
				}
			}
			pack();
			this.setSize(804, 437);
		} catch (Exception e) {
		    //add your error handling code here
			e.printStackTrace();
		}
	}

}
